﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using Bll;
using System.IO;
using System.Collections;

namespace Accounting
{
    public partial class Piece : System.Web.UI.Page
    {
        private int Intnav = -1;
        private int Inttype = -1;
        private string strName = "";
        BllIndex b = new BllIndex();



        ArrayList arrayli = new ArrayList();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBind();
            }
        }

        private void LoadBind()
        {
            ModelIndex m = new ModelIndex();
            if (Request.QueryString["Nav"] != null && Request.QueryString["type"] != null && Request.QueryString["Web"] != null)
            {
                Intnav = int.Parse(Request.QueryString["Nav"].ToString());
                Inttype = int.Parse(Request.QueryString["type"].ToString());
                string web = Request.QueryString["Web"].ToString();
                if (Inttype == 1 && Intnav == 1)
                {
                    Response.Redirect("./Index.aspx");
                }
                else if (web == "")
                {
                    m.Id = Intnav;
                    if (Intnav != -1)
                    {
                        NavLiCss(Intnav);
                        dataBind(m);
                        DivShow(Inttype);
                    }
                }
                else
                    Response.Redirect(web);
            }
            else
            {
                if(Request.QueryString["Web"]!=null)
                {
                    Response.Redirect(Request.QueryString["Web"].ToString());
                }
                else
                    Response.Redirect("./Index.aspx");
            }   
        }

        private void QueryStringBind()
        {
            if (Request.QueryString["Nav"] != null && Request.QueryString["type"] != null && Request.QueryString["Web"] != null)
            {
                Intnav = int.Parse(Request.QueryString["Nav"].ToString());
                Inttype = int.Parse(Request.QueryString["type"].ToString());
            }
        }

        private void DivShow(int i)
        {
            if (i == 1)
            {
                ContentType1.Visible = true;
                ContentType2.Visible = false;
                ContentType3.Visible = false;
                ContentType4.Visible = false;
                ContentType5.Visible = false;
            }
            if (i == 2)
            {
                ContentType1.Visible = false;
                ContentType2.Visible = true;
                ContentType3.Visible = false;
                ContentType4.Visible = false;
                ContentType5.Visible = false;
            }
            if (i == 3)
            {
                ContentType1.Visible = false;
                ContentType2.Visible = false;
                ContentType3.Visible = true;
                ContentType4.Visible = false;
                ContentType5.Visible = false;
            }
            if (i == 4)
            {
                ContentType1.Visible = false;
                ContentType2.Visible = false;
                ContentType3.Visible = false;
                ContentType4.Visible = true;
                ContentType5.Visible = false;
            }
            if (i == 6 || i == 5||i==7)
            {
                ContentType1.Visible = false;
                ContentType2.Visible = false;
                ContentType3.Visible = false;
                ContentType4.Visible = false;
                ContentType5.Visible = true;
            }
        }
          
        private void dataBind(ModelIndex m)
        {
            DataTable bSubNav2 = b.SubNav_Name_All(m);
            DataTable dtNav = b.Nav_Name_All();

            //主要内容地址导航开始
            //if (Inttype == 3 || Inttype == 5)
            //    lblSiteMap.Text = "通知公告";
            //else
            //{
            //    if(Inttype==7)
            //    {
            //        lblSiteMap.Text = "服务指南";
            //    }
            //    else
            //        lblSiteMap.Text = dtNav.Rows[(m.Id - 1)][1].ToString();
            //}
                
            //主要内容地址导航结束

            //主要内容左侧标题开始 
            NavSubLeft.DataSource = b.SubNav_Name_All(m);
            NavSubLeft.DataBind();
            //主要内容左侧标题结束


            //主要内容显示数据绑定开始
            if (Inttype == 1)
            {
                RpPieceContent.DataSource = b.SubNav_Name_All(m);
                RpPieceContent.DataBind();
            }
            if (Inttype == 2)
            {
                if (Request.QueryString["name"] != null)
                {
                    majorDivNP.Visible = true;
                    m.Name = Request.QueryString["name"].ToString();
                    //m.Id = int.Parse(lblSubNavId.Text);
                    m.SqlId = Inttype;

                    DivShow(m.SqlId);
                    //DivShow(2);

                    //浏览次数
                    Number(m);

                    //内容
                    RpPieceContent2.DataSource = b.All_Information(m);
                    RpPieceContent2.DataBind();

                    Previous.InnerHtml = "上一篇：没有了";
                    Next.InnerHtml = "下一篇：没有了";

                }
                else
                {
                    majorDivNP.Visible = false;
                }
            }
            if (Inttype == 3)
            {
                //m.Id = int.Parse(lblSubNavId.Text);
                m.SqlId = Inttype;
                
                DataTable dtNotice = b.All_Information(m);
                
                GVNotic.DataSource = dtNotice;
                GVNotic.DataBind();
            }

            if (Inttype == 4)
            {
                if (Request.QueryString["name"] != null)
                {
                    m.Name = Request.QueryString["name"].ToString();
                    m.SubId = int.Parse(Request.QueryString["subid"].ToString());
                    m.SqlId = Inttype;
                    m.NavId = Intnav.ToString();
                    strName = m.Name;
                    DivShow(m.SqlId);

                    lblSubNavShowName1.Text = strName;

                    m.SqlId = 4;
                    ContentBind(m);

                    
                }
            }

            if (Inttype == 5)
            {
                if (Request.QueryString["Id"] != null)
                {


                    m.SqlId = Inttype;
                    m.Id = int.Parse(Request.QueryString["Id"].ToString());
                    m.NavId = Request.QueryString["Nav"].ToString();

                    //浏览次数
                    Number(m);

                    //内容
                    RpPieceContent5.DataSource = b.All_Information(m);
                    RpPieceContent5.DataBind();

                    //上下篇
                    m.SqlId = 3;

                    NextPrevious(m);
                }
            }

            if (Inttype == 6)
            {
                if (Request.QueryString["Id"] != null)
                {
                    //LinkButton3.Visible = true;
                    m.Id = int.Parse(Request.QueryString["Id"].ToString());
                    m.SqlId = Inttype;
                    m.NavId = Request.QueryString["Nav"];
                    //地址栏绑定开始

                    //lblSubSiteMap.Text = b.SubNav_Name_Singlex(m);
                    //m.Name = lblSubSiteMap.Text;
                    //浏览次数
                    Number(m);

                    //内容绑定开始
                    DataTable dt5 = b.All_Information(m);

                    RpPieceContent5.DataSource = dt5;
                    RpPieceContent5.DataBind();


                    foreach (DataRow my in dt5.Rows)
                    {
                        if (my["FileName"].ToString() == "")
                            my["FileName"] = "无";
                    }

                    //上下篇
                    m.SqlId = 4;

                    NextPrevious(m);

                }
            }

            if (Inttype == 7)
            {
                BllService bservice = new BllService();
                ModelPublic Mpublic = new ModelPublic();

                if (Request.QueryString["Id"] != null)
                {
                    Mpublic.Id = Request.QueryString["Id"].ToString();
                    //地址栏绑定开始
                    
                    
                    //浏览次数
                    bservice.Browse_Frequency(Mpublic);

                    //内容绑定开始
                    DataTable dt5 = bservice.Service_Inquire_Single(Mpublic);

                    RpPieceContent5.DataSource = dt5;
                    RpPieceContent5.DataBind();


                    foreach (DataRow my in dt5.Rows)
                    {
                        if (my["FileName"].ToString() == "")
                            my["FileName"] = "无";
                    }
                    

                }
            }

            //主要内容显示数据绑定结束
        }

        private void NextPrevious(ModelIndex m)
        {
            int NextId = 0;//下一条ID
            int PreviousId = 0;//上一条ID
            int NowId = m.Id;//当前ID

            DataTable dtPN = b.All_Information(m);
            setArray(m);
            ArrayList Noticelist = (ArrayList)Session["number"];

            if (m.SqlId == 3)
                m.SqlId = 7;
            if (m.SqlId == 4)
                m.SqlId = 8;

            NowId = Noticelist.IndexOf(NowId);//返回当前ID位置
            int a = NowId - 1;


            if (a > -1)
            {
                PreviousId = Convert.ToInt16(Noticelist[a]);

                
                m.Id = PreviousId;
                DataTable dtNotice = b.All_Information(m);
                Previous.InnerHtml = "<a href='./Piece.aspx?Type=" + Inttype + "&Nav=" + Intnav + "&Id=" + PreviousId + "&Web='>上一篇：" + dtNotice.Rows[0][1].ToString() + "</a>";
            }
            else
            {
                Previous.InnerHtml = "上一篇：没有了";
            }

            a = NowId + 1;

            if (a < Noticelist.Count)
            {
                NextId = Convert.ToInt16(Noticelist[a]);
                
                m.Id = NextId;
                DataTable dtNotice = b.All_Information(m);
                Next.InnerHtml = "<a href='./Piece.aspx?Type=" + Inttype + "&Nav=" + Intnav + "&Id=" + NextId + "&Web='>下一篇：" + dtNotice.Rows[0][1].ToString() + "</a>";
            }
            else
            {
                Next.InnerHtml = "下一篇：没有了";
            }
        }

        private void setArray(ModelIndex m)
        {
            Session["number"] = null;
            arrayli.Clear();
            

            DataTable dt = b.All_Information(m);

            int i = 1;
            foreach (DataRow my in dt.Rows)
            {
                int num = int.Parse(my["Id"].ToString());
                arrayli.Add(num);
                i++;
            }

            Session["number"] = arrayli;
        }

        private void Number(ModelIndex m)
        {
            b.Browse_Frequency(m);
        }

        private void NavLiCss(int j)
        {
            //导航栏当前页样式名称开始
            string NavName = "";
            if (j != 11)
            {
                for (int i = 1; i < 10; i++)
                {

                    NavName = "LiNavName" + i;
                    if (i == j)
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl(NavName)).Attributes.Add("class", "LiNavNameSelectedCss");
                    else
                        ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl(NavName)).Attributes.Add("class", "LiNavNameCss");


                }
            }
            else
                ((System.Web.UI.HtmlControls.HtmlGenericControl)Master.FindControl("LiNavName1")).Attributes.Add("class", "LiNavNameSelectedCss");

            //导航栏当前页样式名称结束
        }

        private void ContentBind(ModelIndex m)
       {

            DataTable dtInformation= b.All_Information(m);

            GVDepartment.DataSource = dtInformation;
            GVDepartment.DataBind();


            
        }

        protected void NavSubLeft_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            QueryStringBind();
            //左侧菜单点击效果
            Label lblNavSubName = (Label)e.Item.FindControl("lblsubNavName");
            Label lblSubNavId = (Label)e.Item.FindControl("lblSubNavId");

            if (Inttype == 2)
                Response.Redirect("./Piece.aspx?type=2&Nav=" + Intnav + "&name=" + lblNavSubName.Text + "&Web=");
            else if (Inttype == 3 || Inttype == 5)
                Response.Redirect("./Piece.aspx?type=3&Nav=" + Intnav + "&name=" + lblNavSubName.Text + "&Web=");
            else
                Response.Redirect("./Piece.aspx?type=4&Nav=" + Intnav + "&name=" + lblNavSubName.Text + "&Web=&subid="+ lblSubNavId.Text);
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            QueryStringBind();
            //地址导航二级
            if (Inttype == 2)
                Response.Redirect("./Piece.aspx?type=2&Nav=" + Intnav + "&Web=");
            else if (Inttype == 3 || Inttype == 5)
                Response.Redirect("./Piece.aspx?type=3&Nav=" + Intnav + "&Web=");
            else
                Response.Redirect("./Piece.aspx?type=1&Nav=" + Intnav + "&Web=");
        }

        //protected void LinkButton3_Click(object sender, EventArgs e)
        //{
        //    LoadBind();
        //    //地址导航三级

        //    if (Inttype == 2)
        //        Response.Redirect("./Piece.aspx?type=2&Nav=" + Intnav + "&name=" + lblSubSiteMap.Text + "&Web=");
        //    else if (Inttype == 3 || Inttype == 5)
        //        Response.Redirect("./Piece.aspx?type=3&Nav=" + Intnav + "&name=" + lblSubSiteMap.Text + "&Web=");
        //    else
        //        Response.Redirect("./Piece.aspx?type=4&Nav=" + Intnav + "&name=" + lblSubSiteMap.Text + "&Web=");
        //}

        protected void RpPieceContent_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //Repeater 内嵌套Repeater
            if (Inttype == 1)
            {
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                {
                    Repeater replist = e.Item.FindControl("RpPieceContentList") as Repeater;
                    DataRowView rowv = (DataRowView)e.Item.DataItem;
                    //int id = Convert.ToInt32(rowv["Id"]);
                    string name = Convert.ToString(rowv["Name"]);
                    ModelIndex m = new ModelIndex();
                    //m.Id = id;
                    m.Name = name;
                    m.SqlId = Inttype;

                    replist.DataSource = b.All_Information(m);
                    replist.DataBind();
                }
            }
        }

        protected void GVDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
            }
        }

        protected void GVDepartment_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            
            this.GVDepartment.PageIndex = e.NewPageIndex;
            LoadBind();
        }

        protected void RpPieceContent5_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            //文件下载



            LinkButton FileName = (LinkButton)e.Item.FindControl("lbtnFileName");
            Label FileRoute = (Label)e.Item.FindControl("lblFileRoute");


            string fileroute = FileRoute.Text;
            int routeNum = 0;
            for (int i = 0; i < fileroute.Length; i++)
            {
                if (fileroute[i].ToString() == ".")
                {
                    routeNum = i;
                }
            }
            string Suffix = "";
            for (int i = routeNum; i < fileroute.Length; i++)
            {
                Suffix += fileroute[i];

            }

            string filename = FileName.Text + Suffix;
            string path = Server.MapPath("./"+FileRoute.Text);
            FileInfo fi = new FileInfo(path);
            if (fi.Exists)
            {
                Response.Clear();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));
                Response.AddHeader("Content-Length", fi.Length.ToString());
                Response.ContentType = "application/octet-stream;charset=gb2321";
                Response.WriteFile(fi.FullName);
                Response.Flush();
                Response.Close();
            }
        }

        protected void GVNotic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
            }
        }

        protected void GVNotic_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            //ModelIndex m = new ModelIndex();
            //m.SqlId = Inttype;

            this.GVNotic.PageIndex = e.NewPageIndex;
            //dataBind(m);
            LoadBind();
        }


    }
}